using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlashFX : MonoBehaviour
{
    private ControllerBase controller;
    private SpriteRenderer sr;
    [SerializeField]private Material flashMat;
    private Material oldMt;
    private void Awake()
    {
        controller = GetComponent<ControllerBase>();
        sr = GetComponentInChildren<SpriteRenderer>();
        oldMt = sr.material;
    }

    #region 受伤

        private IEnumerator Flash(float sec = 0.15f)
        {
            sr.material = flashMat;
            Color currColor = sr.color;
            sr.color = Color.white;
            yield return new WaitForSeconds(sec);
            sr.color = currColor;
            sr.material = oldMt;
        }
        
        public void TriggerFlash(float sec = 0.15f)
        {
            if (controller.IsDead())
            {
                return;
            }
            StartCoroutine(Flash(sec));
        }
    

    #endregion

    #region 眩晕

    private void Stun()
    {
        if (sr.color!=Color.white)
        {
            sr.color = Color.white;
        }
        else
        {
            sr.color = Color.red;
        }
    }
    
    public void TriggerStun(float sec = 0.1f)
    {
        InvokeRepeating("Stun",0,sec);
    }
    
    public void StopStun()
    {
        sr.color = Color.white;
        CancelInvoke("Stun");
    }

    #endregion

    #region 异常状态
    
    public void TriggerFire()
    {
        TriggerElement(Setting.fireColor, "Fire");
    }

    public void TriggerIce()
    {
        TriggerElement(Setting.iceColor, "Ice");
    }

    public void TriggerLightning()
    {
        TriggerElement(Setting.electricColor, "Lightning");
    }

    private void TriggerElement(Color color, string methodName)
    {
        sr.color = color;
        InvokeRepeating(methodName, 0, 0.3f);
    }

    private void Fire()
    {
        ToggleColor(Setting.fireColor);
    }

    private void Ice()
    {
        ToggleColor(Setting.iceColor);
    }

    private void Lightning()
    {
        ToggleColor(Setting.electricColor);
    }

    private void ToggleColor(Color elementColor)
    {
        sr.color = sr.color == Color.white ? elementColor : Color.white;
    }

    private void StopElement(string methodName)
    {
        sr.color = Color.white;
        CancelInvoke(methodName);
    }

    public void StopFire()
    {
        StopElement("Fire");
    }

    public void StopIce()
    {
        StopElement("Ice");
    }

    public void StopLightning()
    {
        StopElement("Lightning");
    }

    #endregion
}
